<?php


namespace app\admin\service\setting;

use app\common\model\notice\NoticeConfig;
use tmcore\base\BaseService;
use tmcore\exceptions\ValidateException;

class NoticeService extends BaseService
{
    /**
     * 消息管理列表
     * @return array
     */
    public static function lists()
    {
        // 搜索条件
        $searchParam = [
            '=' => ['status','type'],
        ];
        $where = self::getSearchData($searchParam);

        return NoticeConfig::where($where)
            ->field('id,scene,name,type,is_system,is_sms,is_mail,is_wechat,is_applet,is_captcha,status')
            ->order([ 'id' => 'desc'])
            ->select()
            ->toArray();
    }

    /**
     * 编辑消息管理
     * @param $params
     * @return bool
     */
    public static function edit($params)
    {
        self::beginTrans();
        try {

            $template_params = NoticeConfig::getIdentifyParams($params);
            $data = [];

            switch ($params['identify']) {
                case 'system':
                    $data['is_system'] = $params['status'];
                    $data['system_template'] = $template_params;
                    break;
                case 'sms':
                    $data['is_sms'] = $params['status'];
                    $data['sms_template'] = $template_params;
                    break;
                case 'mail':
                    $data['is_mail'] = $params['status'];
                    $data['mail_template'] = $template_params;
                    break;
                case 'wechat':
                    $data['is_wechat'] = $params['status'];
                    $data['wechat_template'] = $template_params;
                    break;
                case 'applet':
                    $data['is_applet'] = $params['status'];
                    $data['applet_template'] = $template_params;
                    break;
                default:
                    throw new ValidateException('消息渠道参数有误');
                    break;
            }

            NoticeConfig::update($data,['id' => $params['id']]);

            self::commitTrans();
            return true;
        } catch (\Exception $e) {
            self::rollbackTrans();
            return self::setError($e->getMessage());
        }
    }

    /**
     * 消息管理详情
     * @param $params
     * @return mixed
     */
    public static function detail($params)
    {
        $row = NoticeConfig::where(['id' => $params['id']])->findOrEmpty()->toArray();

        if(empty($params['identify'])) {
            return $row;
        } else {
            $data = [];
            switch ($params['identify']) {
                case 'system':
                    $data = $row['system_template'];
                    $data['status'] = $row['is_system'];
                    break;
                case 'sms':
                    $data = $row['sms_template'];
                    $data['status'] = $row['is_sms'];
                    break;
                case 'mail':
                    $data = $row['mail_template'];
                    $data['status'] = $row['is_mail'];
                    break;
                case 'wechat':
                    $data = $row['wechat_template'];
                    $data['status'] = $row['is_wechat'];
                    break;
                case 'applet':
                    $data = $row['applet_template'];
                    $data['status'] = $row['is_applet'];
                    break;
                default:
                    break;
            }
            $data['id'] = $row['id'];
            return $data;
        }
    }

    /**
     * 切换消息渠道状态
     * @param $params
     * @return bool
     */
    public static function change($params)
    {
        self::beginTrans();
        try {

            $data = [];

            switch ($params['identify']) {
                case 'system':
                    $data['is_system'] = $params['status'];
                    break;
                case 'sms':
                    $data['is_sms'] = $params['status'];
                    break;
                case 'mail':
                    $data['is_mail'] = $params['status'];
                    break;
                case 'wechat':
                    $data['is_wechat'] = $params['status'];
                    break;
                case 'applet':
                    $data['is_applet'] = $params['status'];
                    break;
                default:
                    throw new ValidateException('消息渠道参数有误');
                    break;
            }

            NoticeConfig::update($data,['id' => $params['id']]);

            self::commitTrans();
            return true;
        } catch (\Exception $e) {
            self::rollbackTrans();
            return self::setError($e->getMessage());
        }
    }
}